
class Caesar:
    _alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

    @staticmethod
    def Encrypt(text, inc):
        return Caesar._caesar(1, text, inc, '')

    @staticmethod
    def Decrypt(text, inc):
        return Caesar._caesar(-1, text, inc, '')

    @staticmethod
    def _caesar(encdec, text, inc, key):
        s = ''
        alphabet = Caesar._alphabet
        inc = inc * 1
        key = Caesar._makeKeyedAlphabet(key, alphabet)
        if encdec < 0:
            inc = len(alphabet) - inc
            b = key
            key = alphabet
            alphabet = b
        for i in range(len(text)):
            b = text[i]
            if (idx := alphabet.find(b)) >= 0:
                idx = (idx + inc) % len(alphabet)
                b = key[idx]
            elif (idx := alphabet.find(b.upper())) >= 0:
                idx = (idx + inc) % len(alphabet)
                b = key[idx].lower()
            s += b
        return s

    @staticmethod
    def _makeKeyedAlphabet(key, alphabet):
        out = ''
        if not isinstance(alphabet, str):
            alphabet = Caesar._alphabet
        else:
            alphabet = alphabet.upper()

        if not isinstance(key, str):
            return alphabet

        key = key.upper() + alphabet
        for i in range(len(key)):
            if out.find(key[i]) < 0 and alphabet.find(key[i]) >= 0:
                out += key[i]
        return out



